import request from '@/utils/request'
import type { ApiResponse, PageResult } from '@/types/common'
import type { PaymentQuery, PaymentRecord, RefundForm, PaymentStats, RefundApproveForm } from '@/types/payment'
import type { AxiosResponse } from 'axios'
import type { Counselor, CounselorPrice } from '@/types/counselor'

// 创建支付订单
export function createPayment(appointmentInfo: string): Promise<{ code: number; data: string; message: string }> {
    return request({
      url: '/payment/create',
      method: 'post',
      data: appointmentInfo
    })
  }

// 获取支付记录列表
export function getPaymentList(params: PaymentQuery) {
  return request<any, AxiosResponse<PageResult<PaymentRecord>>>({
    url: '/payment/list',
    method: 'get',
    params
  })
}

// 获取支付记录详情
export function getPaymentDetail(id: number) {
  return request<any, AxiosResponse<ApiResponse<PaymentRecord>>>({
    url: `/payment/${id}`,
    method: 'get'
  })
}

// 根据订单号查询支付记录
export function getPaymentByOrderNo(orderNo: string) {
  return request<any, AxiosResponse<ApiResponse<PaymentRecord>>>({
    url: `/payment/order/${orderNo}`,
    method: 'get'
  })
}

// 申请退款
export function applyRefund(data: RefundForm): Promise<ApiResponse<void>> {
  return request({
    url: '/payment/refund',
    method: 'post',
    data
  })
}

// 获取支付统计数据
export function getPaymentStats(params: {
  startDate?: string
  endDate?: string
}) {
  return request<any, AxiosResponse<ApiResponse<PaymentStats>>>({
    url: '/payment/stats',
    method: 'get',
    params
  })
}

// 获取咨询师收入统计
export function getCounselorIncome(counselorId: number, params: {
  startDate?: string
  endDate?: string
}) {
  return request({
    url: `/payment/counselor/${counselorId}/income`,
    method: 'get',
    params
  })
}

// 获取平台收入统计
export function getPlatformIncome(params: {
  startDate?: string
  endDate?: string
}) {
  return request({
    url: '/payment/platform/income',
    method: 'get',
    params
  })
}

// 导出支付记录
export function exportPaymentRecords(params: PaymentQuery) {
  return request({
    url: '/payment/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

// 获取支付二维码
export function getPaymentQRCode(orderNo: string) {
  return request<string>({
    url: `/payment/qrcode/${orderNo}`,
    method: 'get'
  })
}

// 查询支付状态
export function checkPaymentStatus(orderNo: string): Promise<ApiResponse<string>> {
  return request({
    url: `/payment/status/${orderNo}`,
    method: 'get'
  })
}

// 获取收入列表
export function getIncomeList(params: PaymentQuery) {
  return request<any, AxiosResponse<PageResult<PaymentRecord>>>({
    url: '/payment/income/list',
    method: 'get',
    params
  })
}

// 分发收入
export function distributeIncome(data: {
  orderNo?: string
  counselorName?: string
  counselorAmount?: number
  remark?: string
  records?: Array<{
    orderNo: string
    counselorName: string
    counselorAmount: number
  }>
}) {
  return request<any, AxiosResponse<ApiResponse<void>>>({
    url: '/payment/income/distribute',
    method: 'post',
    data
  })
}

// 获取收入统计数据
export function getIncomeStatistics() {
  return request<any>({
    url: '/payment/income/statistics',
    method: 'get'
  })
}

// 设置咨询师价格
export function setCounselorPrice(data: {
  counselorId: number
  pricePerHour: number
  status: string
}) {
  return request<any, AxiosResponse<ApiResponse<void>>>({
    url: '/counselor/price/set',
    method: 'post',
    data
  })
}

// 获取咨询师价格
export function getCounselorPrice(counselorId: number) {
  return request<any, AxiosResponse<CounselorPrice>>({
    url: `/counselor/price/${counselorId}`,
    method: 'get'
  })
}

// 获取所有咨询师列表
export function getCounselorList() {
  return request<any, AxiosResponse<ApiResponse<Counselor[]>>>({
    url: '/counselor/list',
    method: 'get'
  })
}

// 更新咨询师价格
export function updateCounselorPrice(data: {
  id: number      
  counselorId: number
  pricePerHour: number
  status: string
}) {
  return request<any, AxiosResponse<ApiResponse<void>>>({
    url: '/counselor/price/update',
    method: 'put',
    data
  })
}

// 获取平台账户信息
export function getPlatformAccount() {
  return request({
    url: '/platform/account/info',
    method: 'get'
  })
}

// 设置平台费率
export function updatePlatformFeeRate(feeRate: number) {
  return request({
    url: '/platform/account/fee-rate',
    method: 'put',
    params: { feeRate }
  })
}

// 审核退款申请
export function approveRefund(data: RefundApproveForm): Promise<ApiResponse<void>> {
  return request({
    url: '/payment/refund/approve',
    method: 'post',
    params: data
  })
}

// 取消订单
export function cancelPayment(orderNo: string): Promise<ApiResponse<void>> {
  return request({
    url: `/payment/cancel/${orderNo}`,
    method: 'put'
  })
}